package com.example.administrator.daydayappmarket.list.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.BoolRes;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.text.Spanned;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import com.example.administrator.daydayappmarket.Contacts;
import com.example.administrator.daydayappmarket.DownloadActivity;
import com.example.administrator.daydayappmarket.R;
import com.example.administrator.daydayappmarket.SearchActivity;
import com.example.administrator.daydayappmarket.list.model.ListSecondActivityBodyItem;
import com.example.administrator.daydayappmarket.utils.MoreDialog;
import com.google.gson.Gson;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

public class ListSecondBodyActivity extends AppCompatActivity {


    @InjectView(R.id.activity_list_second_body_scrollView)
    ScrollView scrollView;

    @InjectView(R.id.activity_list_second_body_head_iamgeveiw0)
    ImageView imageViewheadback;

    @InjectView(R.id.activity_list_second_body_head_textview0)
    TextView textView0;
    @InjectView(R.id.activity_list_second_body_iamgeView10)
    ImageView imageView0;
    @InjectView(R.id.activity_list_second_body_textview11)
    TextView textView;
    @InjectView(R.id.activity_list_second_body_textview12)
    TextView textView1;
    @InjectView(R.id.activity_list_second_body_textview13)
    TextView textView2;
    @InjectView(R.id.activity_list_second_body_textview14)
    TextView textView3;

    @InjectView(R.id.activity_list_second_body_textview10)
    TextView textView4;

    @InjectView(R.id.activity_list_second_body_YingYong_textview0)
    TextView textView5;
    @InjectView(R.id.activity_list_second_body_YingYong_textview1)
    TextView textView6;
    @InjectView(R.id.activity_list_second_body_YingYong_textview2)
    TextView textView7;

    @InjectView(R.id.activity_list_second_body_HorizontalScrollView0_layout)
    LinearLayout linearLayout0;

    @InjectView(R.id.activity_list_second_body_HorizontalScrollView1_layout)
    LinearLayout linearLayout1;

    //按钮 展开---收起
    @InjectView(R.id.activity_list_second_body_YingYong_button0)
    Button button;



    private String path = null;
    private int tag=0;
    private RequestQueue requestQueue;
    private ListSecondActivityBodyItem.DataBean data;
    private String imageUrl=Contacts.LIST_IAMAGE;

    private int index = 1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_list_second_body);
        ButterKnife.inject(this);

        initdata();

        requestQueue = Volley.newRequestQueue(this);

        requestMethod();

    }

    private void initdata() {
        Intent intent = getIntent();
        path = intent.getStringExtra("path");
        tag=intent.getIntExtra("tag",0);
        data = new ListSecondActivityBodyItem.DataBean();
    }

    private void requestMethod() {

        StringRequest request = new StringRequest(path, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                Gson gson = new Gson();
                ListSecondActivityBodyItem listSecondActivityBodyItem = gson.fromJson(response, ListSecondActivityBodyItem.class);
                data = listSecondActivityBodyItem.getData();

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        textView0.setText(data.getName());
                        textView.setText(data.getName());

                        //
                        if (data.getIcon_url().startsWith("h",0)) {
                            Glide.with(getApplicationContext()).load(data.getIcon_url()).into(imageView0);
                        }else {
                            Glide.with(getApplicationContext()).load(imageUrl+data.getIcon_url()).into(imageView0);
                        }
                        textView1.setText(data.getCategory());

                        //47788501  / 1204     %10  /100
                        int i1 = Integer.parseInt(data.getPackage_size());
                        double v1 = ((int) (((double) i1) / 1024.0 / 1024.0 * 100.0)) / 100.0;
                        textView2.setText(v1+"MB");
                        int i2 = Integer.parseInt(data.getDownload_count());
                        int i3 = i2 / 10000;
                        textView3.setText(i3+"万人下载");

                        textView4.setText("小编点评： "+data.getReviews());

                        textView5.setText(data.getVersion_name());

                        //引用描述 更新
                        String update=data.getCreated_at();
                        String substring = update.substring(0, update.indexOf(" "));
                        String replace = substring.replace("-", "/");
                        textView6.setText(replace);

                        //展开 收起 监听器
                        final Spanned spanned = Html.fromHtml(data.getDescription());
                        textView7.setText(spanned);
                        button.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                switch (index){
                                    case 0:
                                        textView7.setMaxLines(2);
                                        textView7.setText(spanned);//转换-------------
                                        index =1;

                                        button.setText("展开");
                                        Drawable drawable = ContextCompat.getDrawable(getApplicationContext(), R.drawable.lsit_second_boty_login_open);
                                        drawable.setBounds(1,1,50,50);
                                        button.setCompoundDrawables(null,null,drawable ,null);
                                        break;
                                    case 1:
                                        index=0;
                                        textView7.setMaxLines(20);
                                        textView7.setText(spanned);//转换-------------

                                        button.setText("收起");
                                        Drawable drawable1 = ContextCompat.getDrawable(getApplicationContext(), R.drawable.list_second_body_login_close);
                                        drawable1.setBounds(1,1,50,50);
                                        button.setCompoundDrawables(null,null,drawable1 ,null);
                                        break;
                                }
                            }
                        });



                        //中间图片
                        for (int i = 0; i < data.getScreenshoot_urls().size(); i++) {
                            View view= LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_activity_list_second_body_horiablescollview_first,null);
                            ImageView imageViewFirst= (ImageView) view.findViewById(R.id.item_lsit_solft_body_horiableView_imageview);
                            if (data.getScreenshoot_urls().get(i).startsWith("h",0)){
                                Glide.with(getApplicationContext()).load(data.getScreenshoot_urls().get(i)).into(imageViewFirst);
                            }else {
                                Glide.with(getApplicationContext()).load(imageUrl+data.getScreenshoot_urls().get(i)).into(imageViewFirst);
                            }
                            linearLayout0.addView(view);
                        }


                        //底部horizonble
                        for (int i = 0; i < data.getRecommend_apps().size(); i++) {
                            final ListSecondActivityBodyItem.DataBean.RecommendAppsBean bean=data.getRecommend_apps().get(i);
                            View view1=LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_activity_list_second_body_horiablescollview_second,null);

                            LinearLayout linearLayout= (LinearLayout) view1.findViewById(R.id.item_second_activty_lsit_solft_body_horiableView_second_layout);

                            ImageView imageviewSecond= (ImageView) view1.findViewById(R.id.item_second_activty_lsit_solft_body_horiableView_second_imageview);
                            TextView   textViewFirst= (TextView) view1.findViewById(R.id.item_second_activty_lsit_solft_body_horiableView_second_textView0);
                            TextView  textViewSecond= (TextView) view1.findViewById(R.id.item_second_activty_lsit_solft_body_horiableView_second_textView1);

                            textViewFirst.setText(bean.getName());
                            int i4 = Integer.parseInt(bean.getPackage_size());
                            double v3 = ((int) (((double) i4) / 1024.0 / 1024.0 * 100.0)) / 100.0;
                            textViewSecond.setText(v3+"MB");
                            if (bean.getIcon_url().startsWith("h",0)){
                                Glide.with(getApplicationContext()).load(bean.getIcon_url()).into(imageviewSecond);
                            }else {
                                Glide.with(getApplicationContext()).load(imageUrl+bean.getIcon_url()).into(imageviewSecond);
                            }
//                            view1.setTag(bean.getName());

                            //底部layout监听
                            linearLayout.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Intent intent=new Intent(getApplicationContext(),ListSecondBodyActivity.class);
                                    switch (tag) {
                                        case 2:
                                            intent.putExtra("path",Contacts.LIST_SECOND+bean.getId());
                                            intent.putExtra("tag",2);
                                            break;
                                        case 1:
                                            intent.putExtra("path",Contacts.LIST_SECOND_GAME+bean.getId());
                                            intent.putExtra("tag",1);
                                            break;

                                    }
                                    startActivity(intent);
                                }
                            });

                            linearLayout1.addView(view1);

                        }



                    }
                });

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        request.setTag("sd");
        requestQueue.add(request);
        requestQueue.start();

    }


    //退出监听
    @OnClick({R.id.activity_list_second_body_head_iamgeveiw0,R.id.activity_list_second_body_head_imagview2,
            R.id.activity_list_second_body_head_iamgeviewSearch,R.id.activity_list_second_body_head_imagview1
            ,R.id.activity_list_second_body_button})
    public void onclick(View view){

        switch (view.getId()) {

            //头部返回键
            case R.id.activity_list_second_body_head_iamgeveiw0:
                finish();
                break;
            //头部下载建
            case R.id.activity_list_second_body_head_imagview2:
                Intent intent = new Intent(this, DownloadActivity.class);
                startActivity(intent);
                break;
            //头部搜索键
            case R.id.activity_list_second_body_head_iamgeviewSearch:
                Intent intent1 = new Intent(this, SearchActivity.class);
                startActivity(intent1);
                break;
            //头部更多键
            case R.id.activity_list_second_body_head_imagview1:

//                listSceondmoreDialog();

                new  MoreDialog(this).listSceondmoreDialog();

                break;
            //底部下载 监听器
            case R.id.activity_list_second_body_button:


//                scrollView.scrollTo();

                break;
        }


    }

    private void listSceondmoreDialog() {
        final AlertDialog alertDialog=new AlertDialog.Builder(this, R.style.list_second_draw_dialog).create();
        View view1= LayoutInflater.from(ListSecondBodyActivity.this).inflate(R.layout.list_second_body_dialog_more,null);
        alertDialog.setView(view1);

        //底部出现的动画
        Window window = alertDialog.getWindow();
        window.setGravity(Gravity.BOTTOM);  //此处可以设置dialog显示的位置
        window.setWindowAnimations(R.style.mystyle);
        alertDialog.show();

        //xx监听器 实现退出对话框
        ImageView imageView= (ImageView) view1.findViewById(R.id.list_second_body_dialog_more_imageview);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.reset(this);
        requestQueue.cancelAll("sd");
    }
}
